
import 'package:flutter/material.dart';

import 'simple_app_upgrade.dart';

enum DownloadStatus{
  ///
  /// 未下载
  ///
  none,
  ///
  /// 准备开始下载，请求网络到下载第1个字节之间
  ///
  start,
  ///
  /// 正在下载
  ///
  downloading,
  ///
  /// 下载完成
  ///
  done,
  ///
  /// 下载异常
  ///
  error
}

class AppUpgradeInfo {
  AppUpgradeInfo({required this.appName, required this.version, required this.title, required this.contents, required this.appDownloadUrl, required this.force, required this.appDownloadName});

  ///
  /// title,显示在提示框顶部
  ///
  final String title;

  ///
  /// 升级内容
  ///
  final List<String> contents;

  ///
  /// apk下载url
  ///
  final String appDownloadUrl;

  ///
  /// 是否强制升级
  ///
  final bool force;

  final String version;

  final String appName;

  final String appDownloadName;
}

///
/// 下载进度回调
///
typedef DownloadProgressCallback = Function(int count, int total);

///
/// 下载状态变化回调
///
typedef DownloadStatusChangeCallback = Function(DownloadStatus downloadStatus, {dynamic error});

class AppUpgrade {
static appUpgrade(
    BuildContext context,
    AppUpgradeInfo appUpgradeInfo, {
    TextStyle? titleStyle,
    TextStyle? contentStyle,
    String cancelText = "以后再说",
    TextStyle? cancelTextStyle,
    String okText = "",
    TextStyle? okTextStyle,
    Color? progressBarColor,
    String iosAppId="立即更新",
    VoidCallback? onCancel,
    VoidCallback? onOk,
    DownloadProgressCallback? downloadProgress,
    DownloadStatusChangeCallback? downloadStatusChange,
  }) {
    return _showUpgradeDialog(
            context, appUpgradeInfo.title, appUpgradeInfo.contents,
            apkDownloadUrl: appUpgradeInfo.appDownloadUrl,
            force: appUpgradeInfo.force,
            titleStyle: titleStyle,
            contentStyle: contentStyle,
            cancelText: cancelText,
            cancelTextStyle: cancelTextStyle,
            okText: okText,
            okTextStyle: okTextStyle,
            downloadApkName: appUpgradeInfo.appDownloadName,
            onCancel: onCancel,
            onOk: onOk,
            downloadProgress: downloadProgress,
            downloadStatusChange: downloadStatusChange);
  }


  ///
  /// 展示app升级提示框
  ///
  static _showUpgradeDialog(
    BuildContext context,
    String title,
    List<String> contents, {
    required String apkDownloadUrl,
    bool force = false,
    TextStyle? titleStyle,
    TextStyle? contentStyle,
    required String cancelText,
    TextStyle? cancelTextStyle,
    required String okText,
    TextStyle? okTextStyle,
    required String downloadApkName,
    VoidCallback? onCancel,
    VoidCallback? onOk,
    DownloadProgressCallback? downloadProgress,
    DownloadStatusChangeCallback? downloadStatusChange,
  }) {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) {
          return WillPopScope(
            onWillPop: () async {
              return false;
            },
            child: Dialog(
                shape: const RoundedRectangleBorder(
                    borderRadius:
                        BorderRadius.all(Radius.circular(3.0))),
                child: SimpleAppUpgradeWidget(
                  title: title,
                  titleStyle: titleStyle,
                  contents: contents,
                  contentStyle: contentStyle,
                  cancelText: cancelText,
                  cancelTextStyle: cancelTextStyle,
                  okText: okText,
                  okTextStyle: okTextStyle,
                  downloadUrl: apkDownloadUrl,
                  force: force,
                  downloadApkName: downloadApkName,
                  onCancel: onCancel,
                  onOk: onOk,
                  downloadProgress: downloadProgress,
                  downloadStatusChange: downloadStatusChange
                )),
          );
        });
  }
}